﻿using System.Web.UI;
using KamLib;

namespace Ext.Net.UIS
{

    public interface ITermBox
    {
        TermNode ParentTermNode { get; set; }
        TermNode TermNode { get; set; }
        string Name { get; set; }
        Term Term { get; set; }

        /// <summary>
        /// Создаёт новые control-s, и вставляет их сразу вслед за текущим компонентом.
        /// </summary>
        void CreateControls(UisCreateControlArgs e);

        bool ControlsCreated { get; set; }
        Context Context { get; set; }
    }

    public class UisCreateControlArgs
    {
        public UisCreateControlArgs(Context context) { Context = context; }
        
        public Context Context;

        /// <summary>
        /// Передаётся для присвоения создаваемым компонентам свойств, указанных для FieldBox
        /// </summary>
        public StateBag BaseViewState;
    }


    public static class TermBoxHelper
    {

        public static TermNode UisGetTermNode(this AbstractComponent cmp)
        {
            var box = cmp as ITermBox;
            if (box == null) return null;

            var term = box.Term;
            var termNode = box.TermNode;
            var parentNode = box.ParentTermNode;

            if (term == null && box.Name.yes())
            {
                box.Term = term = Term.Get(box.Name, parentNode);
            }

            if (termNode == null)
            {
                box.TermNode = termNode = new TermNode(box.Context, parentNode, term);
            }
            else
            {
                box.Term = term = termNode.Term;
                box.ParentTermNode = termNode.Parent;
            }

            if (term != null && box.Name.no())
            {
                box.Name = term.Name;
            }


            return termNode;
        }


        public static void UisAddTermNodeConfigItem(this Ext.Net.AbstractComponent cmp)
        {
            var box = cmp as ITermBox;
            if (box == null || box.TermNode == null) return;

            cmp.CustomConfig.Add(new ConfigItem("termNode", box.TermNode.ID, ParameterMode.Value));
        }


        /// <summary>
        /// Увеличивает длину всех Fields на LabelWidth
        /// </summary>
        public static void ApplyLabelWidthToFields(this AbstractContainer cmp, int defaultLabelWidthAndPad = 100 + 5)
        {
            var labelWidth = cmp.GetLabelWidthAndPad(defaultLabelWidthAndPad);

            foreach (var item in cmp.Items)
            {
                var fld = item as Ext.Net.Field;
                if (fld != null)
                {
                    if (fld.Width.IsEmpty) continue;
                    var box = fld as ITermBox;
                    if (box != null && box.TermNode != null && !box.TermNode.HidingTitle)
                        fld.Width = (int)fld.Width.Value + (fld.LabelWidth != 100 ? fld.LabelWidth : labelWidth);
                }
                else
                {
                    var cnt = item as Ext.Net.AbstractContainer;
                    if (cnt != null)
                        ApplyLabelWidthToFields(cnt, labelWidth);
                }
            }
        }
    }



}
